/*
 * Copyright (C) 2011 Christopher Probst
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of the 'FoxNet Codec' nor the names of its 
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.foxnet.codec.impl;

import java.io.ObjectStreamClass;
import java.lang.reflect.Method;

import com.foxnet.codec.Codec;
import com.foxnet.codec.ObjectInputChannel;
import com.foxnet.codec.ObjectInstantiator;
import com.foxnet.codec.ObjectOutputChannel;
import com.foxnet.codec.SunObjectInstantiator;

/**
 * @author Christopher Probst
 */
public class CodecApp {

    public static class T implements Codec<Object> {
	/*
	 * (non-Javadoc)
	 * @see
	 * com.foxnet.codec.Codec#decode(com.foxnet.codec.ObjectInputChannel,
	 * java.lang.Object)
	 */
	@Override
	public boolean decode(ObjectInputChannel oic, Object obj)
		throws Exception {
	    return false;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * com.foxnet.codec.Codec#encode(com.foxnet.codec.ObjectOutputChannel,
	 * java.lang.Object)
	 */
	@Override
	public boolean encode(ObjectOutputChannel ooc, Object obj)
		throws Exception {
	    return false;
	}
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {

	// ReflectionFactory rf = ReflectionFactory.getReflectionFactory();
	// String s =
	// (String) rf.newConstructorForSerialization(String.class,
	// Object.class.getConstructor()).newInstance();
	//
	// System.out.println(System.identityHashCode(s));

	ObjectInstantiator oi = new SunObjectInstantiator();
	oi.newInstance(String.class);

	Method newInstance =
		ObjectStreamClass.class.getDeclaredMethod("newInstance");
	newInstance.setAccessible(true);

	long d = System.nanoTime();

	for (int i = 0; i < 1000000; i++) {
	    oi.newInstance(String.class);

	    // ObjectStreamClass c = ObjectStreamClass.lookup(String.class);
	    // newInstance.invoke(c);
	}

	d = System.nanoTime() - d;
	System.out.println((d / 1000000d) + " ns");

	// CodecRegistry cr = new CodecRegistry();
	// cr.bind(T.class);
	// System.out.println(cr.bind(T.class));
	// System.out.println(cr.unbind(T.class));
	//
	// Thread.sleep(10000);

	// System.out.println("test");
	//
	// ByteBuffer b = ByteBuffer.allocateDirect(1024 * 1024 * 200);
	//
	// CodecRegistry sys = new DefaultIoSystem();
	//
	// // Create new ostream!
	// ObjectOutputChannel<?> ob = sys.newOStream(ByteBuffer.class);
	//
	// ob.setResource(b);
	//
	// String[] strings = new String[1000000];
	// for (int i = 0; i < strings.length; i++) {
	// strings[i] = "" + i;
	// }
	//
	// long[] longs = new long[20000000];
	//
	// long d = System.nanoTime();
	//
	// //
	// // for (int i = 0; i < longs.length; i++) {
	// // ob.writeLong(longs[i]);
	// // }
	//
	// ob.writeObject(strings);
	//
	// double time = (System.nanoTime() - d) * 0.000001d;
	// System.out.println("Time (ms): " + time);

    }
}
